const fp = require('lodash/fp');
const { Maybe, Container } = require('./support')

const splitStr = _str => str => `${_str}${str}`
const title = splitStr('3-')
const _l = defaultString => (str, ...args) => console.log.apply(null, [`${defaultString}`, ...args]);
const l = (str, ...arg) => (_l(`${title(str)}: => `))(str, ...arg);

// 第三题：3-1
let maybe = Maybe.of([5, 6, 1])
let ex1 = () => {
    return maybe.map(fp.map(fp.add(10)))._value
}
l(1, ex1())

// 第三题：3-2
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = () => {
    return xs.map(fp.first)._value
}
l(2, ex2())

// 第三题：3-3
let safeProp = fp.curry(function (x, o) {
    return Maybe.of(o[x])
})
let user = { id: 2, name: 'Albert' }
let ex3 = () => {
    return safeProp('name')(user).map(fp.first)._value
}
l(3, ex3())

// 第三题：3-3
let ex4 = (n) => {
    if (n) {
        return parseInt(n)
    }
}
ex4 = (n) => {
    return Maybe.of(n).map(parseInt)._value
}
l(4, ex4(null), ex4(10))
